package com.hsshy.beam.seckill.aspectj;
import com.hsshy.beam.common.utils.R;
import com.hsshy.beam.common.utils.RedisUtil;
import com.hsshy.beam.common.utils.ToolUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Scope
@Aspect
@Order(1)
public class NoRepeatSubmitAspect {

	@Autowired
	private RedisUtil redisUtil;

	//Service层切点     用于记录错误日志
	@Pointcut("@annotation(com.hsshy.beam.common.annotion.NoRepeatSubmit)")
	public void lockAspect() {
		
	}
	
    @Around("lockAspect()")
    public  Object around(ProceedingJoinPoint joinPoint) {
		Object obj = null;
		String key1 = joinPoint.getArgs()[0].toString();
		String key2 = joinPoint.getArgs()[1].toString();
		if(ToolUtil.isNotEmpty(redisUtil.get("repeat_"+key1+":"+key2))){
			System.out.println("重复订单："+key2);
			return R.fail("订单重复提交");
		}
		else {
			redisUtil.set("repeat_"+key1+":"+key2,"1",2L);
			try {

					obj = joinPoint.proceed();


			} catch (Throwable e) {
				e.printStackTrace();
				throw new RuntimeException();
			} finally{

			}
			return obj;
		}

    }
}
